package com.gray.quartz.wheel;

import com.gray.quartz.simple.SimpleQuartz;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created on 2021/6/7.
 *
 * @author grayCat
 * @since 1.0
 */
@RestController
public class WheelControllerTest {

    @Autowired
    WheelManager wheelManager;

    @RequestMapping("/whell/{maxNum}")
    public void simple(@PathVariable long maxNum) throws SchedulerException {
        wheelManager.changeState(true);

        Thread top = Thread.currentThread();
        System.out.println("topName:" + top.getName()+ "topId:" + top.getId());
        AtomicInteger value1 = new AtomicInteger(0);
        AtomicInteger value2 = new AtomicInteger(0);
        AtomicInteger value3 = new AtomicInteger(0);
        AtomicInteger value4 = new AtomicInteger(0);
        long startTime = new Date().getTime();

        Runnable runnable = () ->{
            long endTime = new Date().getTime();
            long time = endTime - startTime - 2000;
            if(time <= 1000) {
                value1.addAndGet(1);
            }else if (time <= 2000){
                value2.addAndGet(1);
            }else if (time <= 3000){
                value3.addAndGet(1);
            }else {
                value4.addAndGet(1);
            }
        };

        //200w有延迟
        for(int i = 0;i < maxNum ;i ++){
            wheelManager.add(runnable,2000);
        }

        try {
            Thread.sleep(10000);
            System.out.println("1miao : " + value1.get());
            System.out.println("2miao : " + value2.get());
            System.out.println("3miao : " + value3.get());
            System.out.println("nmiao : " + value4.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

//        try {
//            Thread.sleep(10000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
    }
}
